package com.example.demo.controller;

import com.example.demo.common.AjaxResult;
import com.example.demo.common.Constant;
import com.example.demo.common.SecurityUtil;
import com.example.demo.common.SessionUtil;
import com.example.demo.model.ArticleInfo;
import com.example.demo.model.UserInfo;
import com.example.demo.service.ArticleService;
import com.example.demo.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private ArticleService articleService;


    @RequestMapping("/reg")
    public Object reg(String username,String password){

        //1.非空校验
        if(!StringUtils.hasLength(username)||!StringUtils.hasLength(password)){
            return AjaxResult.fail("非法的请求参数",-1);
        }

        //查询该用户名是否被占用
        UserInfo res=userService.getUserByName(username);
        if(res!=null){

            return AjaxResult.fail("该用户名已被占用",-2);
        }

        //2.进行添加操作
        int result=userService.add(username,SecurityUtil.encrypt(password));
        System.out.println("插入结果:"+result);
        if(result==1){
            return AjaxResult.success("添加成功",1);
        }
        return AjaxResult.fail("数据库添加失败",-1);
    }


    @RequestMapping("/login")
    public int login(HttpServletRequest request, String username, String password){

        //1.非空校验
        if(!StringUtils.hasLength(username)||!StringUtils.hasLength(password)){
            return 0;
        }
        //2.查询
        UserInfo userInfo=userService.getUserByName(username);

        if(userInfo==null||userInfo.getId()<=0){
            return 0;
        }else{
            boolean result= SecurityUtil.decrypt(password,userInfo.getPassword());
            if(result){
                //登录成功
                //将userinfo保存到session中
                HttpSession session=request.getSession();
                session.setAttribute(Constant.SESSION_USERINFO_KEY,userInfo);
                return 1;
            }
        }
        return 0;

    }

    @RequestMapping("/myinfo")
    public UserInfo myInfo(HttpServletRequest request){
        UserInfo userInfo=SessionUtil.getLoginUser(request);
        if(userInfo!=null){
            //根据用户名查询文章数量
            userInfo.setAcount(articleService.getCountArticleByName(userInfo.getId()));
            return userInfo;
        }
        return null;
    }
    @RequestMapping("/myinfobyuid")
    public  UserInfo getUserByUid(Integer uid){
        UserInfo userInfo=userService.getUserByUid(uid);
        if(userInfo==null||userInfo.getId()<=0){
            return null;
        }
        return userInfo;

    }

    /**
     * 退出登录
     * @return
     */
    @RequestMapping("/logout")
    public boolean logout(HttpServletRequest request){
        HttpSession session= request.getSession(false);
        if(session!=null&&session.getAttribute(Constant.SESSION_USERINFO_KEY)!=null){
            session.removeAttribute(Constant.SESSION_USERINFO_KEY);
        }
        return true;
    }


}
